package cn.wenhaha.app.log.service.imp;

import cn.wenhaha.app.log.SyncLogReportHandler;
import cn.wenhaha.app.log.bean.LogMapping;
import cn.wenhaha.app.log.bean.LogRow;
import cn.wenhaha.app.log.bean.ReportErrorType;
import cn.wenhaha.app.log.service.SyncLogService;
import cn.wenhaha.sync.core.SyncErrorReport;
import cn.wenhaha.sync.core.SyncReport;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * --------
 *
 * @author ：wyndem
 * @Date ：Created in 2022-12-18 17:52
 */
@Service
public class SyncLogServiceImp  implements SyncLogService {

    private SyncLogReportHandler logReportHandler;

    public SyncLogServiceImp(SyncLogReportHandler logReportHandler) {
        this.logReportHandler = logReportHandler;
    }

    @Override
    public void add(LogMapping logMapping, List<SyncReport> reportList, ReportErrorType type) {
        for (SyncReport report : reportList) {

            LogRow logRow = LogRow.builder()
                    .mappingId(logMapping.getMappingId())
                    .mappingName(logMapping.getMappingName())
                    .nameData(logMapping.getNameData())
                    .syncReport(report)
                    .nameUpload(logMapping.getNameUpload()).build();

            if (report instanceof SyncErrorReport){
                logRow.setErrorType(type);
            }
            logReportHandler.add(logRow);
        }


    }
}
